#!/bin/bash

EXE_NAME="./sonicomSecureClient -s"
KILL_EXE="pkill sonicomEncrypt & pkill sonicomSecure"
#KILL_EXE="pkill sonicomEncryptVpn & pkill sonicomSecureClient"  #looks pkill command limit the number of the target string


SYS_VERSION=""
sys_name=""
CENTOS="cent"
UBUNTU="ubuntu"
DEBIAN="debian"
lib_crypto="/lib64/libcrypto.so.1.0.0"
lib_ssl="/lib64/libssl.so.1.0.0"


function deal_libfile()
{
    SYS_VERSION=`cat /proc/version`
    sys_name=$(echo $SYS_VERSION | grep -i "${CENTOS}")
    echo "[SONICOM] $SYS_VERSION"
    if [ "$sys_name" != "" ];then
        #cent os
        echo "[SONICOM] sytem os verison: Centos"
        #file exsit or not? if file exist, do noting. or copy libs file to /lib64/
        if [ ! -f "$lib_crypto" ]; then
               # cp ./libs/libssl.so.1.0.0 /lib64/
                cp ./libs/libcrypto.so.1.0.0 /lib64/
                #echo    "cp ./libs/libssl.so.1.0.0 /lib64/"
                echo    "cp ./libs/libcrypto.so.1.0.0 /lib64/"
        else
                 echo "[SONICOM] file /lib64/libcrypto.so.1.0.0 exsit"
        fi
        return
    fi

    sys_name=$(echo $SYS_VERSION | grep -i "${UBUNTU}")
    if  [ "$sys_name" != "" ];then
        #debian os
        echo "[SONICOM] Sytem os verison release: Debian"
        #file exsit or not? if file exist, do noting. or copy libs file to /lib64/
        if [ ! -f "$lib_crypto" ]; then
               # cp ./libs/libssl.so.1.0.0 /lib64/
                cp ./libs/libcrypto.so.1.0.0 /lib64/
                #echo    "cp ./libs/libssl.so.1.0.0 /lib64/"
                echo "[SONICOM] cp ./libs/libcrypto.so.1.0.0 /lib64/"
        else
                echo "[SONICOM] file /lib64/libcrypto.so.1.0.0 exsit "
        fi
        return
    fi
}


#***********************************************************************************
#para correct or not , if get wrong para exit
if [ "$1" != "start" ] &&  [ "$1" != "stop" ] &&  [ "$1" != "help" ]; then
	echo "[SONICOM] Please enter the correct parameters!"
	exit 0
fi
#***********************************************************************************
if [ "$1" == "help" ] ; then
    echo "[SONICOM] You can enter: $0 start|stop|help"
	exit 0
fi




#******************************************************************************@@@@@@@@@@@@@@@*****
# process running or not
count_sonicomvpn=`ps -ef | grep -i "sonicomEncryptVpn" |wc -l`
count_secureClient=`ps -ef | grep -i "sonicomSecureClient" |wc -l`
if [ "$1" == "start" ] ; then
    echo "[SONICOM] Start sonicomEncryptVpn service..."
    #deal_libfile
    sleep 2
    #process is running or not, running echo and exit 0.  else start
    if [ $count_secureClient -gt 1 ] || [ $count_sonicomvpn -gt 1 ];then
        echo "[SONICOM] process is running!! You can try execute COMMAND: $0 stop"
    else
        echo "[SONICOM] Start work process..."
        eval $EXE_NAME
        sleep 3
        #jude start sucessful or not
        count_sonicomvpn=`ps -ef | grep -i "sonicomEncryptVpn" |wc -l`
        count_secureClient=`ps -ef | grep -i "sonicomSecureClient" |wc -l`
        if [ $count_secureClient -gt 1 ] && [ $count_sonicomvpn -gt 1 ];then
            echo "[SONICOM] Start work process sucess!"
        else
            echo "[SONICOM] Start work process failed!"
            exit 0
        fi
        exit 0
    fi
fi

if [ "$1" == "stop" ] ; then
    echo "[SONICOM] Stop sonicomEncryptVpn service..."
      #process is running or not, running echo and exit 0.  else start
    if [ $count_secureClient -gt 1 ] || [ $count_sonicomvpn -gt 1 ];then
        echo "[SONICOM] Stop work process..."
        eval $KILL_EXE
        sleep 2
        #jude stop sucessful or not
        count_sonicomvpn=`ps -ef | grep -i "sonicomEncryptVpn" |wc -l`
        count_secureClient=`ps -ef | grep -i "sonicomSecureClient" |wc -l`
        if [ $count_secureClient -gt 1 ] && [ $count_sonicomvpn -gt 1 ];then
            echo "[SONICOM] Stop work process failed! You should check log."
        else
            echo "[SONICOM] Stop work process sucess!"
            exit 0
        fi
    else
        echo "[SONICOM] noting to Stop !!!"
    fi
	exit 0
fi

#******************************************************************************@@@@@@@@@@@@@@@*****

